// 购物车模块
import request from '@/utils/request';
import { defineStore } from 'pinia'
import { IResponse } from '@/types/data';
import { CartItem } from '@/types/cart';
import useStore from '@/store';
const useCartStore = defineStore('cart', {
    // 开启自动持久化插件
    persist: true,
    // 状态
    state() {
        return {
            list: [] as CartItem[], // 购物车列表
        }
    },
    // 方法
    actions: {
        // 加入购物车
        async addCart(data:CartItem) {
            if(this.isLogin) {
                const {skuId,count} = data
                await request.post('/member/cart', {skuId,count})
                // 添加购物车成功后需要重新渲染购物车列表
                this.getCartList()
            } else {
                const goods = this.list.find((item) => item.skuId === data.skuId)
                if (goods) {
                    goods.count += data.count
                } else {
                    this.list.unshift( data )
                }
            }
        },
        // 获取购物车列表
        async getCartList() {
            if(this.isLogin) {
                const res = await request.get<IResponse<CartItem[]>>('/member/cart')
                this.list = res.data.result
            }else {
                // 遍历发送请求, 校验更新sku商品的库存和价格, 是否有效
                this.list.forEach(async (cartItem) => {
                    const { skuId } = cartItem
                    // 根据 skuId 获取最新商品信息
                    const res = await request.get<IResponse<CartItem>>(
                        `/goods/stock/${skuId}`
                    )
                    // 保存最新商品信息
                    const lastCartInfo = res.data.result
                    // 更新商品现价
                    cartItem.nowPrice = lastCartInfo.nowPrice
                    // 更新商品库存
                    cartItem.stock = lastCartInfo.stock
                    // 更新商品是否有效
                    cartItem.isEffective = lastCartInfo.isEffective
                })
            }
        },
        // 删除购物车数据
        async deleteCart(skuIds: string[]) {
           if(this.isLogin) {
            await request.delete('/member/cart', {
                data: { ids: skuIds }
            })
            // 删除购物车数据后要重新获取购物车列表
            this.getCartList()
           }else {
            this.list = this.list.filter(item => !skuIds.includes(item.skuId))
           }
        },
        // 更新选中状态
        async updateCart(skuId: string,data: { selected?: boolean; count?: number }) {
            if(this.isLogin) {
                await request.put(`/member/cart/${skuId}`, data)
                // 重新获取最新购物车列表
                this.getCartList()
            }else {
                const goods = this.effectiveList.find((item) => item.skuId === skuId)
                if (data.selected !== undefined) {
                    goods!.selected = data.selected
                }
                if (data.count !== undefined) {
                    goods!.count = data.count
                }
            }
        },
        // 修改全选或者全不选
        async updateCartAllSelected(isSelected: boolean) {
           if(this.isLogin) {
                await request.put('/member/cart/selected', {
                    selected: isSelected,
                })
                // 重新获取购物车列表
                this.getCartList()
           }else {
                this.list.forEach((item) => {
                    item.selected = isSelected
                })
           }
        },
        // 清空购物车
        clearCart() {
            this.list = []
        },
        // 合并购物车
    async mergeLocalCart() {
        const data = this.list.map(({ skuId, selected, count }) => ({
            skuId,
            selected,
            count
        }))
        await request.post('/member/cart/merge', data)
        // 合并成功，重新获取购物车列表
        this.getCartList()
    }
    },
    // 计算
    getters: {
        // 有效商品列表 ->  isEffective为true并且库存大于0才有效
        effectiveList(): CartItem[] {
            return this.list.filter((item) => item.stock > 0 && item.isEffective)
        },
        // 有效商品总数量 ->  把effctiveList中的每一项的count 相加
        effectiveListCounts(): number {
            return this.effectiveList.reduce((sum, item) => sum + item.count, 0)
        },
        // 总价格 -> 有效商品每一项的单价 * 有效商品的每一项count 然后相加
        effectiveListPrice(): string {
            return this.effectiveList
            .reduce((sum, item) => sum + item.count * Number(item.nowPrice), 0)
            .toFixed(2)
        },
        // 全选状态
        isAllSelected(): boolean {
            // effectiveList.length不为0
            return (this.effectiveList.length !== 0 && this.effectiveList.every((item) => item.selected)) 
        },
        // 已选择的列表
        selectedList(): CartItem[] {
            return this.effectiveList.filter((item) => item.selected)
        },
        // 已选择的商品总数
        selectedListCounts(): number {
            return this.selectedList.reduce((sum, item) => sum + item.count, 0)
        },
        // 已选择的列表总价
        selectedListPrice(): string {
            return this.selectedList
            .reduce((sum, item) => sum + item.count * Number(item.nowPrice), 0)
            .toFixed(2)
        },
        // 是否登录
        isLogin(): boolean {
            const { user } = useStore()
            return !! user.profile.token
        }
    },
})

export default useCartStore